import { AV, request, pageQuery } from "./leanCloud";

export default function(name) {
  let query = new AV.Query(name);
  const obj = {};

  // 分页查询
  obj.pageQuery = function(params, pageNum, pageSize) {
    return request(pageQuery(query, params, pageNum, pageSize));
  };

  //   增加
  obj.create = function(data) {
    var object = new AV.Object(name);
    for (let key in data) {
      object.set(key, data[key]);
    }
    return request(object.save());
  };

  // 查询详情
  obj.queryInfoById = function(objectId) {
    return request(query.get(objectId));
  };

  // 更新
  obj.update = async function(data) {
    var object = await query.get(data.objectId);
    delete data.objectId;
    delete data.createdAt;
    delete data.updatedAt;

    for (let key in data) {
      console.log(key);
      object.set(key, data[key]);
    }
    console.log(object);
    return request(object.save());
  };

  // 删除
  obj.remove = async function(data) {
    return request((await query.get(data.objectId)).destroy());
  };

  return obj;
}
